<template>
  <view class="container">
    <view v-if="sts == 0">
      <view class="pay-sts fail">支付失败</view>
      <view class="tips">
        请在
        <text class="warn">30分钟</text>
        内完成付款
      </view>
      <view class="tips">过期订单将会被系统取消</view>
      <view class="btns">
        <text class="button checkorder" @tap="onOrderList">查看订单</text>
        <text class="button payagain" @tap="onRepay">重新支付</text>
      </view>
    </view>

    <view v-if="sts == 1">
      <view class="pay-sts success">支付成功</view>
      <view class="tips">感谢您的支持</view>
      <view class="btns">
        <text class="button checkorder" @tap="onOrderList">查看订单</text>
        <text class="button shopcontinue" @tap="onIndex">继续购物</text>
      </view>
    </view>
  </view>
</template>

<script setup>
const sts = ref(0);
const orderNumbers = ref('');
const productName = ref('');
const amount = ref('');

onLoad((options) => {
  sts.value = options.sts;
  orderNumbers.value = options.orderNumbers;
  productName.value = options.productName;
  amount.value = options.amount;
});

const onOrderList = () => {
  uni.navigateTo({
    url: '/pages/user/orderList/orderList?sts=0'
  });
};

const onIndex = () => {
  uni.switchTab({
    url: '/pages/index/index'
  });
};

const onRepay = () => {
  uni.showLoading({
    mask: true
  });
  http
    .request({
      url: '/p/pay/prepay',
      method: 'POST',
      data: {
        orderNumbers: orderNumbers.value,
        body: productName.value,
        amount: amount.value,
        openid: uni.getStorageSync('openId')
      }
    })
    .then(({ data }) => {
      uni.requestPayment({
        provider: 'wxpay',
        timeStamp: data.timeStamp,
        nonceStr: data.nonceStr,
        package: data.packageVal,
        signType: data.signType,
        paySign: data.paySign,
        success: () => {
          uni.hideLoading();
          uni.redirectTo({
            url: '/pages/index/payResult/payResult?sts=1&orderNum=' + orderNumbers.value
          });
        }
      });
    })
    .finally(() => {
      uni.hideLoading();
    });
};
</script>

<style lang="scss" scoped>
@use './payResult.scss';
</style>
